//
//  HWQQGameHistoryController.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/12/12.
//  Copyright © 2023 94hwan. All rights reserved.
//

import UIKit

class HWQQGameHistoryController: HWBaseViewController {
    var tableView:UITableView!
    var tableViewProtocol = HWQQGameHistoryProtocol()
    var httpManager = HWQQGameTypeHttpManager()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addAction()
    }

}

extension HWQQGameHistoryController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "历史记录"
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
        
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
}

extension HWQQGameHistoryController {
    func addAction() -> Void {
        tableViewProtocol.gameButtonClick = {
            mod in
            self.httpManager.getQQGameClickUpLoad(gameId: mod.appid) { success in
                
            } failure: { errorString in
                
            }
            QQMiniAppSDKManager.sharedInstance().showApplication(withAppID: mod.appid, scene: .MAEntrySceneNone, parentVC: self, hostModel: nil)
            
            let historyModel = HWQQGameHistoryListModel()
            historyModel.id = mod.id
            historyModel.name = mod.name
            historyModel.icon = mod.icon
            historyModel.content = mod.content
            historyModel.appid = mod.appid
            historyModel.type = mod.type
            historyModel.biaoqian = mod.biaoqian
            historyModel.time = mod.time
            
            //最多只显示五条最新数据
            if HWDataBaseManager.defaultManger.selectAll().count == 5 {
                HWDataBaseManager.defaultManger.deleteWithSql(sql: "id in (select id from QQGameHistory where userId = '\(HWLocallyStoredData.getLoginUserID())' order by id ASC limit 1)") {
                    
                } failBlock: {
                    
                }
                
            }
            HWDataBaseManager.defaultManger.delete(appId: historyModel.appid) {
                
            } failBlock: {
                
            }
            HWDataBaseManager.defaultManger.insert(appId:historyModel.appid, model: historyModel) {
                
            } failBlock: {
                
            }
        }
    }
}
